import { createStore } from 'vuex'


export default createStore({
    state: {
        Authorization: localStorage.getItem('Authorization') ? localStorage.getItem('Authorization') : ''
    },
    mutations: {
    // 进行数据更新，改变数据状态
        // 修改token，并将token存入localStorage
        changeLogin (state, user) {
            state.Authorization = user.Authorization;
            localStorage.setItem('Authorization', user.Authorization);
        },
    },
    actions: {
    //执行动作，数据请求

    },
    getters: {
    // 获取到最终的数据结果

    }
})
import axios from 'axios';
axios.interceptors.request.use(
    config => {
      if (localStorage.getItem('Authorization')) {
        config.headers.Authorization = localStorage.getItem('Authorization');
      }
   
      return config;
    },
    error => {      
      return Promise.reject(error);
    });

  axios.interceptors.response.use(function (response) {
      return response;
  }, function (error) {
    if (error.response.status === 401) {
      localStorage.removeItem('Authorization');
      this.$router.push('/login');
    }
      console.log("响应拦截器出错",error);
  });
